#include "pod_parser.h"
#include <iostream>
#include <sstream>

bool PodParser::ParsePod(const std::string& json, nanok::Pod& pod) {
    Json::Value root;
    Json::CharReaderBuilder builder;
    std::istringstream iss(json);
    std::string errs;
    
    if (!Json::parseFromStream(builder, iss, &root, &errs)) {
        std::cerr << "Failed to parse JSON: " << errs << std::endl;
        return false;
    }
    
    try {
        // 解析 metadata
        if (root.isMember("metadata")) {
            Json::Value metadata = root["metadata"];
            pod.metadata.name = metadata["name"].asString();
            pod.metadata.namespace_ = metadata["namespace"].asString();
            pod.metadata.resourceVersion = metadata["resourceVersion"].asInt64();
        }
        
        // 解析 spec
        if (root.isMember("spec")) {
            Json::Value spec = root["spec"];
            if (spec.isMember("containers")) {
                Json::Value containers = spec["containers"];
                for (const auto& container : containers) {
                    nanok::Container c;
                    c.name = container["name"].asString();
                    c.image = container["image"].asString();
                    c.port = container["port"].asInt();
                    pod.spec.containers.push_back(c);
                }
            }
        }
        
        // 解析 status
        if (root.isMember("status")) {
            Json::Value status = root["status"];
            pod.status.phase = status["phase"].asString();
            pod.status.hostIP = status["hostIP"].asString();
            pod.status.podIP = status["podIP"].asString();
            pod.status.startTime = status["startTime"].asString();
        }
        
        return true;
    } catch (const std::exception& e) {
        std::cerr << "Error parsing Pod: " << e.what() << std::endl;
        return false;
    }
}

bool PodParser::ParsePodList(const std::string& json, std::vector<nanok::Pod>& pods) {
    Json::Value root;
    Json::CharReaderBuilder builder;
    std::istringstream iss(json);
    std::string errs;
    
    if (!Json::parseFromStream(builder, iss, &root, &errs)) {
        std::cerr << "Failed to parse JSON: " << errs << std::endl;
        return false;
    }
    
    try {
        // 检查是否是数组
        if (root.isArray()) {
            for (const auto& item : root) {
                nanok::Pod pod;
                // 将每个数组元素转换为字符串并解析为 Pod
                Json::StreamWriterBuilder writer;
                std::string podJson = Json::writeString(writer, item);
                if (ParsePod(podJson, pod)) {
                    pods.push_back(pod);
                }
            }
            return true;
        }
        
        // 如果不是数组，尝试解析为单个 Pod
        nanok::Pod pod;
        if (ParsePod(json, pod)) {
            pods.push_back(pod);
            return true;
        }
        
        return false;
    } catch (const std::exception& e) {
        std::cerr << "Error parsing Pod list: " << e.what() << std::endl;
        return false;
    }
}

std::string PodParser::ToJson(const nanok::Pod& pod) {
    Json::Value root;
    
    // 构建 metadata
    Json::Value metadata;
    metadata["name"] = pod.metadata.name;
    metadata["namespace"] = pod.metadata.namespace_;
    metadata["resourceVersion"] = pod.metadata.resourceVersion;
    root["metadata"] = metadata;
    
    // 构建 spec
    Json::Value spec;
    Json::Value containers(Json::arrayValue);
    for (const auto& container : pod.spec.containers) {
        Json::Value c;
        c["name"] = container.name;
        c["image"] = container.image;
        c["port"] = container.port;
        containers.append(c);
    }
    spec["containers"] = containers;
    root["spec"] = spec;
    
    // 构建 status
    Json::Value status;
    status["phase"] = pod.status.phase;
    status["hostIP"] = pod.status.hostIP;
    status["podIP"] = pod.status.podIP;
    status["startTime"] = pod.status.startTime;
    root["status"] = status;
    
    Json::StreamWriterBuilder builder;
    return Json::writeString(builder, root);
}